package service

import (
	"blog/app/models"
	XzaimsModel "blog/app/models/xzaims_model"
	"blog/config"
	"blog/utils"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

// GetTags 获取标签列表
func GetTags(c *gin.Context) {
	var tags []models.Tags
	//err := utils.MySqlDB.Model(&tags).Find(&tags).Error
	err := utils.GetLocalMysql(utils.Xzaims).Model(&tags).Find(&tags).Error
	if err != nil {
		fmt.Println("错误", err)
	}
	c.JSON(config.SUCCESS, gin.H{
		config.ResCode: config.SUCCESS,
		config.ResMsg:  config.GetErrMsg(config.SUCCESS),
		"tag_list":     tags,
	})
}

// GetUserName 获取用户的名字
func GetUserName(c *gin.Context) {
	var users []models.User
	//err := utils.MySqlDB.Model(&users).Find(&users).Error
	//err := utils.GetLocalMysql(utils.Xzaims).Model(&users).Find(&users).Error
	// 更新用户筛选逻辑
	LocalDB := utils.GetLocalMysql(utils.Xzaims).Model(&users)
	err := LocalDB.Find(&users).Error

	if err != nil {
		fmt.Println("cuowu", err)
	}
	var resUsers []string
	for _, v := range users {
		resUsers = append(resUsers, fmt.Sprintf("%s(%s)", v.UserName, v.UserChineseName))
		// 改成返回userid
		//resUsers = append(resUsers, v.UserID)
	}

	c.JSON(config.SUCCESS, gin.H{
		config.ResCode: config.SUCCESS,
		config.ResMsg:  config.GetErrMsg(config.SUCCESS),
		"user_names":   resUsers,
	})
}

type ReqParam struct {
	Username string `json:"username"`
}

// TagsAdminSystem 获取 user_tags 和 system_admin 连表结果
type TagsAdminSystem struct {
	UserID          string `gorm:"column:user_id;type:varchar(64);not null" json:"user_id"`
	UserName        string `gorm:"column:user_name;type:varchar(128);not null" json:"user_name"`
	UserChineseName string `gorm:"column:user_chinese_name;type:varchar(64)" json:"user_chinese_name"`
	Phone           string `gorm:"column:phone;type:varchar(64)" json:"phone"`
	Status          int    `gorm:"column:status;type:tinyint(4);not null" json:"status"`
}

// GetUserListByAdminTags 根据admin获取标签返回 user 列表 - user_tags
func GetUserListByAdminTags(c *gin.Context) {
	// 搜索用户名返回信息
	var param ReqParam
	if err := c.ShouldBindJSON(&param); err != nil {
		fmt.Println("绑定信息失败")
		c.JSON(http.StatusOK, gin.H{"msg": "bind json is 失败"})
	}
	fmt.Println(param.Username)
	var systemData []XzaimsModel.UserTag

	LocalDB := utils.GetLocalMysql(utils.Xzaims).Model(&systemData)
	err := LocalDB.Find(&systemData).Error
	if err != nil {
		fmt.Println("获取sys的db数据失败")
		c.JSON(http.StatusOK, gin.H{"msg": "bind json is 失败"})
	}
	var resultAdminTags []TagsAdminSystem
	GetDB := utils.GetLocalMysql(utils.Xzaims)
	GetDB.Table("user").Select("user.user_id,user.user_name,user.user_chinese_name,user.phone,ut.status,ut.tag_id").
		Joins("left join user_tags ut on user.user_id = ut.user_id").
		Where("ut.status = 1").
		Where("user.user_name like ? ", "%"+param.Username+"%").
		Scan(&resultAdminTags)

	c.JSON(http.StatusOK, gin.H{
		"msg":  "bind json is ok",
		"data": resultAdminTags,
	})
}

// GetSystemAdmin 根据admin获取标签返回 user 列表
func GetSystemAdmin(c *gin.Context) {
	// 搜索用户名返回信息
	var param ReqParam
	if err := c.ShouldBindJSON(&param); err != nil {
		fmt.Println("绑定信息失败")
		c.JSON(http.StatusOK, gin.H{"msg": "bind json is 失败"})
	}
	fmt.Println(param.Username)
	var systemData []XzaimsModel.SystemAdmin
	LocalDB := utils.GetLocalMysql(utils.Xzaims).Model(&systemData)
	err := LocalDB.Find(&systemData).Error
	if err != nil {
		fmt.Println("获取sys的db数据失败")
		c.JSON(http.StatusOK, gin.H{"msg": "bind json is 失败"})
	}

	c.JSON(http.StatusOK, gin.H{
		"msg":  "bind json is ok",
		"data": systemData,
	})
}

// ReadFile 读取文件
func ReadFile(c *gin.Context) {
	//searchKeyContent, err := ioutil.ReadFile("static/json/searchKey.txt")
}
